<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;


use app\admin\model\Changyong;
use app\admin\model\Member;
use app\admin\model\RoleModel;
use app\admin\model\UserModel;
use think\Controller;
use think\Db;
use think\Request;

class Members extends Controller
{
    //用户列表
    public function index()
    {
        $param = input('param.');
        if(request()->isAjax()){

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['user_name|phone'] = ['like', '%' . $param['searchText'] . '%'];
            }
            if (!empty($param['searchText']['user_type'])){
                $where['user_type'] = $param['searchText']['user_type'];
            }


            $user = new Member();
            $selectResult = $user->getMemberByWhere($where, $offset, $limit);

            $return['total'] = $user->getAllMembers($where);  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    /**
     * 弹出选择
     */
    public function userlist(Request $request){
        $where =[];
        $where['type'] = ['in','1,2'];
        $param = $request->param();
        if (isset($param['real_name']) && !empty($param['real_name'])) $where['real_name'] = ['like','%'.$param['real_name'].'%'];
        if (isset($param['phone']) && !empty($param['phone'])) $where['phone'] = $param['phone'];
        $data = Db::name('member')->where($where)->paginate();
        $this->assign('list',$data);
        return $this->fetch();
    }


    public function memberlist(Request $request){
        $list = (new Changyong())->getlist();
        $this->assign(compact('list'));
        return $this->fetch();
    }

    public function add(Request $request){
        if ($request->isAjax()){
            return (new Changyong())->add();
        }
        return $this->fetch();
    }


    public function edit(Request $request){
        if ($request->isAjax()){
            return (new  Changyong())->edit();
        }
        $m = Changyong::get($request->param('id'));

        $this->assign(compact('m'));
        return $this->fetch();

    }

    public function del(Request $request){
        if ($request->isAjax()){
            if (Changyong::where('id',$request->param('id'))->delete()){
                return msg(1,url('memberlist'),'提交成功');
            }else{
                return msg(-1,[],'操作失败');
            }
        }
    }





}
